package com.ls.x.dao;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ls.x.activity.beans.AlertModelBean;
import com.ls.x.tool.DatabaseHelper;

public class AlertModeDao implements IAlertModeDao {
	
	private DatabaseHelper databaseHelper;
	
	public AlertModeDao(DatabaseHelper databaseHelper) {
		super();
		this.databaseHelper = databaseHelper;
	}

	@Override
	public AlertModelBean getAlertModeBeanById(int id) {
		
		SQLiteDatabase sd = databaseHelper.getReadableDatabase();
		
//		Cursor cursor = sd.rawQuery("select distinct rm.* from context as c,remind_mode as rm where rm.id=c.mode",
//				null);
		
		Cursor cursor = sd.rawQuery("select * from remind_mode where id="+id,
				null);
		
		AlertModelBean alertModelBean = new AlertModelBean();

		int alertModeId = cursor.getInt(cursor
				.getColumnIndex("id"));
		String name = cursor.getString(cursor
				.getColumnIndex("name"));
		String mode = cursor.getString(cursor
				.getColumnIndex("mode"));
		String remindTimes = cursor.getString(cursor
				.getColumnIndex("remind_times"));
		String smsNumber = cursor.getString(cursor
				.getColumnIndex("sms_number"));
		String alertContent = cursor.getString(cursor
				.getColumnIndex("alert_content"));

		alertModelBean.setId(alertModeId);
		alertModelBean.setName(name);
		alertModelBean.setMode(mode);
		alertModelBean.setRemindTimes(remindTimes);
		alertModelBean.setSmsNumber(smsNumber);
		alertModelBean.setAlertContent(alertContent);
		
		cursor.close();
		sd.close();
		
		return alertModelBean;
	}

	@Override
	public List<AlertModelBean> getAllAlertModeBeans() {
		
		List<AlertModelBean> alertModelBeanList = new ArrayList<AlertModelBean>();
		SQLiteDatabase sd = databaseHelper.getReadableDatabase();
		Cursor cursor = sd.rawQuery("select * from remind_mode",
				null);
		while (cursor.moveToNext()) {

			AlertModelBean alertModelBean = new AlertModelBean();

			int alertModeId = cursor.getInt(cursor
					.getColumnIndex("id"));
			String name = cursor.getString(cursor
					.getColumnIndex("name"));
			String mode = cursor.getString(cursor
					.getColumnIndex("mode"));
			String remindTimes = cursor.getString(cursor
					.getColumnIndex("remind_times"));
			String smsNumber = cursor.getString(cursor
					.getColumnIndex("sms_number"));
			String alertContent = cursor.getString(cursor
					.getColumnIndex("alert_content"));

			alertModelBean.setId(alertModeId);
			alertModelBean.setName(name);
			alertModelBean.setMode(mode);
			alertModelBean.setRemindTimes(remindTimes);
			alertModelBean.setSmsNumber(smsNumber);
			alertModelBean.setAlertContent(alertContent);

			alertModelBeanList.add(alertModelBean);
		}
		
		cursor.close();
		sd.close();
		return alertModelBeanList;
	}

}
